import { Vector3 } from "three";

export default class Triangle{
    private pointArray: Array<Vector3>;
    private divisionEdgeLength: number;
    //maxEdge中存储了三角形中长度最大边的相关信息，在maxEdge中的startPoint和endPoint分别表示最大边的起点和终点，middlePoint表示最大边中间的点，edgeLength表示最大边的长度，oppositePoint表示最大边对面的顶点
    private maxEdge: {startPoint: Vector3, endPoint: Vector3, middlePoint: Vector3, edgeLength: number, oppositePoint: Vector3};

    public constructor(pointArray: Array<Vector3>, divisionEdgeLength: number){        
        this.pointArray = pointArray;
        this.divisionEdgeLength = divisionEdgeLength;

        this.maxEdge = {startPoint: null, endPoint: null, middlePoint: null, edgeLength: 0, oppositePoint: null};
        
        // 初始化时计算最大边
        this.calculateMaxEdge();
    }
    
    /**
     * 计算三角形中长度最大的边，并将相关信息存储在maxEdge中
     */
    private calculateMaxEdge(): void {
        if (!this.pointArray || this.pointArray.length < 3) {
            return;
        }
        
        const [p0, p1, p2] = this.pointArray;
        
        // 计算三条边的长度
        const edge01Length = p0.distanceTo(p1);
        const edge12Length = p1.distanceTo(p2);
        const edge20Length = p2.distanceTo(p0);
        
        // 找出最长边并设置maxEdge
        if (edge01Length >= edge12Length && edge01Length >= edge20Length) {
            this.maxEdge.startPoint = p0;
            this.maxEdge.endPoint = p1;
            this.maxEdge.edgeLength = edge01Length;
            this.maxEdge.oppositePoint = p2;
            // 计算最大边的中间点
            this.maxEdge.middlePoint = new Vector3().lerpVectors(p0, p1, 0.5);
        } else if (edge12Length >= edge01Length && edge12Length >= edge20Length) {
            this.maxEdge.startPoint = p1;
            this.maxEdge.endPoint = p2;
            this.maxEdge.edgeLength = edge12Length;
            this.maxEdge.oppositePoint = p0;
            // 计算最大边的中间点
            this.maxEdge.middlePoint = new Vector3().lerpVectors(p1, p2, 0.5);
        } else {
            this.maxEdge.startPoint = p2;
            this.maxEdge.endPoint = p0;
            this.maxEdge.edgeLength = edge20Length;
            this.maxEdge.oppositePoint = p1;
            // 计算最大边的中间点
            this.maxEdge.middlePoint = new Vector3().lerpVectors(p2, p0, 0.5);
        }
    }
    
    /**
     * 获取三角形的最大边信息
     */
    public getMaxEdge(): {startPoint: Vector3, endPoint: Vector3, middlePoint: Vector3, edgeLength: number, oppositePoint: Vector3} {
        return this.maxEdge;
    }
    
    /**
     * 更新三角形的顶点并重新计算最大边
     */
    public updatePoints(pointArray: Array<Vector3>): void {
        this.pointArray = pointArray;
        this.calculateMaxEdge();
    }

    /**
     * 把三角形分割成两个小三角形
     */
    public divide(){
        let pointArrayOne = [this.maxEdge.oppositePoint, this.maxEdge.startPoint, this.maxEdge.middlePoint];
        let triangleOne = new Triangle(pointArrayOne, this.divisionEdgeLength);
        
        let pointArrayTwo = [this.maxEdge.oppositePoint, this.maxEdge.middlePoint, this.maxEdge.endPoint];
        let triangleTwo = new Triangle(pointArrayTwo, this.divisionEdgeLength);

        return [triangleOne, triangleTwo];
    }

    /**
     * 得到三角形的数组列表
     * @returns 返回三角形的数组列表，数组中的元素是Vector3类型的对象
     */
    public getPointArray(){
        return this.pointArray;
    }
}